package com.ourteam.product.model.api;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;

import com.ourteam.product.model.dao.IProductModelServiceParameterDAO;
import com.ourteam.product.model.dao.ProductModelServiceParameterDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelServiceBean;
import com.ourteam.product.model.domain.ProductModelServiceParameterBean;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

public class ProductModelServiceHelper {

	private ProductModelServiceBean modelServiceBean;

	static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory.getProductModelService();

	public ProductModelServiceHelper(ProductModelServiceBean modelServiceBean) {
		super();
		this.modelServiceBean = modelServiceBean;
	}

	public String getServiceName() {
		return this.modelServiceBean.getServiceName();
	}

	public boolean isCanTest() {
		return BooleanUtils.toBoolean(this.modelServiceBean.getCanTest());
	}

	public String getRemarks() {
		return this.modelServiceBean.getRemarks();
	}

	public ProductModelServiceParameterHelper getOutput() throws Exception {

		ProductModelServiceParameterDAOQueryBean queryBean = new ProductModelServiceParameterDAOQueryBean();

		queryBean.createCriteria().andServiceIdEqualTo(this.modelServiceBean.getId()).andParameterTypeEqualTo("OUTPUT");

		ProductModelServiceParameterBean[] parameterBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelServiceParameters(queryBean);

		if (ArrayUtils.isNotEmpty(parameterBeans)) {
			return new ProductModelServiceParameterHelper(parameterBeans[0]);
		} else {
			return null;
		}
	}

	public ProductModelServiceParameterHelper[] getInputList() throws Exception {

		ProductModelServiceParameterDAOQueryBean queryBean = new ProductModelServiceParameterDAOQueryBean();

		queryBean.createCriteria().andServiceIdEqualTo(this.modelServiceBean.getId()).andParameterTypeEqualTo("INPUT");

		queryBean.setSortProperty(IProductModelServiceParameterDAO.SortIndex);

		ProductModelServiceParameterBean[] parameterBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelServiceParameters(queryBean);

		ProductModelServiceParameterHelper[] helpers = new ProductModelServiceParameterHelper[parameterBeans.length];

		for (int i = 0; i < helpers.length; i++) {
			helpers[i] = new ProductModelServiceParameterHelper(parameterBeans[i]);
		}

		return helpers;
	}

}
